/**
 * 
 */
package com.ermax.aircond.common.web.bean;

import java.io.IOException;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ermax.aircond.common.domain.WriteMachineData;
import com.ermax.aircond.common.service.WriteMachineDataService;
import com.ermax.aircond.util.service.MachineCommandService;

/**
 * @author Eric
 *
 */

@Scope("session")
@Component
@ManagedBean(name = "testBean")
public class CommandBean implements Serializable {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= -476200322932685461L;
	@Autowired
	private WriteMachineDataService writeMachineDataService;
	
	@Autowired
	private MachineCommandService machineCommandService;
	
	private static final Logger logger = Logger.getLogger(CommandBean.class);
	
	@NotNull
	@Size(min = 1, max = 3)
	private String nid;

	@NotNull
	@Size(min = 1, max = 64)
	private String cmd;
	

	@Size(min = 1, max = 2)
	private String type;
	
	@Size(min = 0, max = 2)
	private String onOff;
	
	@Size(min = 0, max = 2)
	private String fanSpeed;
	
	@Size(min = 0, max = 2)
	private String roomTemp;
	
	@Size(min = 0, max = 2)
	private String gateTemp;
	
	//@Size(min = 0, max = 2)
	private int defaultTemp;
	
	@Size(min = 1, max = 2)
	private String valveOnOff;
	
	@Size(min = 1, max = 4)
	private String door;
	
	@Size(min = 1, max = 3)
	private String minTemp;
	
	@Size(min = 1, max = 2)
	private String lock;
	
	@Size(min = 1, max = 9)
	private String status;
	
	@Size(min = 1, max = 2)
	private String filter;
	
	private String error;
	
	
	
	
	public String insert() throws IOException, ServletException {
		
		logger.debug("Insert page: " + this.getCmd() + ", " + this.getNid());
		
		WriteMachineData writeMachineData = writeMachineDataService.select(Short.parseShort(this.getNid()));
		
		if (null == writeMachineData) {
			writeMachineData = new WriteMachineData();
			writeMachineData.setCmd(this.getCmd());
			writeMachineData.setNid(Short.parseShort(this.getNid()));
		
			writeMachineDataService.insert(writeMachineData);
		} else {
			writeMachineData.setCmd(this.getCmd());			
			writeMachineDataService.update(writeMachineData);
		}
		
		//HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
		HttpServletResponse  response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
		//HttpSession session = request.getSession(false);
		
		response.sendRedirect("test.jsf");
		return null;
	}

	
	public String placeCommand() throws IOException, ServletException {
		
		
		
		StringBuilder sbCommand = new StringBuilder();
		
		sbCommand.append(machineCommandService.getOnOffString(this.getOnOff()));
		
			sbCommand.append(machineCommandService.getFanSpeedString(this.getFanSpeed()));
		
			sbCommand.append(machineCommandService.getDefaultTempString(String.valueOf(this.getDefaultTemp())));
		
		
		String command = machineCommandService.getCommandString(sbCommand.toString());
		
		logger.debug("Place command: " + this.getNid() + ", " + command);
		
		WriteMachineData writeMachineData = new WriteMachineData();
		writeMachineData.setCmd(this.getCmd());
		writeMachineData.setNid(Short.parseShort(this.getNid()));
		
		writeMachineDataService.update(writeMachineData);
		
		/*
		WriteMachineData writeMachineData = writeMachineDataService.select(Integer.parseInt(this.getNid()));
		
		if (null == writeMachineData) {
			writeMachineData = new WriteMachineData();
			writeMachineData.setCmd(command);
			writeMachineData.setNid(Short.parseShort(this.getNid()));
		
			writeMachineDataService.insert(writeMachineData);
		} else {
			writeMachineData.setCmd(command);			
			writeMachineDataService.update(writeMachineData);
		}
		*/
		HttpServletResponse  response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
		response.sendRedirect("command.jsf");
		return null;
		
	}

	/**
	 * @return the nid
	 */
	public String getNid() {
		return nid;
	}


	/**
	 * @param nid the nid to set
	 */
	public void setNid(String nid) {
		this.nid = nid;
	}


	/**
	 * @return the cmd
	 */
	public String getCmd() {
		return cmd;
	}


	/**
	 * @param cmd the cmd to set
	 */
	public void setCmd(String cmd) {
		this.cmd = cmd;
	}


	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}


	/**
	 * @param type the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}


	/**
	 * @return the onOff
	 */
	public String getOnOff() {
		return onOff;
	}


	/**
	 * @param onOff the onOff to set
	 */
	public void setOnOff(String onOff) {
		this.onOff = onOff;
	}


	/**
	 * @return the fanSpeed
	 */
	public String getFanSpeed() {
		return fanSpeed;
	}


	/**
	 * @param fanSpeed the fanSpeed to set
	 */
	public void setFanSpeed(String fanSpeed) {
		this.fanSpeed = fanSpeed;
	}


	/**
	 * @return the roomTemp
	 */
	public String getRoomTemp() {
		return roomTemp;
	}


	/**
	 * @param roomTemp the roomTemp to set
	 */
	public void setRoomTemp(String roomTemp) {
		this.roomTemp = roomTemp;
	}


	/**
	 * @return the gateTemp
	 */
	public String getGateTemp() {
		return gateTemp;
	}


	/**
	 * @param gateTemp the gateTemp to set
	 */
	public void setGateTemp(String gateTemp) {
		this.gateTemp = gateTemp;
	}


	/**
	 * @return the defaultTemp
	 */
	public int getDefaultTemp() {
		return defaultTemp;
	}


	/**
	 * @param defaultTemp the defaultTemp to set
	 */
	public void setDefaultTemp(int defaultTemp) {
		this.defaultTemp = defaultTemp;
	}


	/**
	 * @return the valveOnOff
	 */
	public String getValveOnOff() {
		return valveOnOff;
	}


	/**
	 * @param valveOnOff the valveOnOff to set
	 */
	public void setValveOnOff(String valveOnOff) {
		this.valveOnOff = valveOnOff;
	}


	/**
	 * @return the door
	 */
	public String getDoor() {
		return door;
	}


	/**
	 * @param door the door to set
	 */
	public void setDoor(String door) {
		this.door = door;
	}


	/**
	 * @return the minTemp
	 */
	public String getMinTemp() {
		return minTemp;
	}


	/**
	 * @param minTemp the minTemp to set
	 */
	public void setMinTemp(String minTemp) {
		this.minTemp = minTemp;
	}


	/**
	 * @return the lock
	 */
	public String getLock() {
		return lock;
	}


	/**
	 * @param lock the lock to set
	 */
	public void setLock(String lock) {
		this.lock = lock;
	}


	/**
	 * @return the status
	 */
	public String getStatus() {
		return status;
	}


	/**
	 * @param status the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}


	/**
	 * @return the filter
	 */
	public String getFilter() {
		return filter;
	}


	/**
	 * @param filter the filter to set
	 */
	public void setFilter(String filter) {
		this.filter = filter;
	}


	/**
	 * @return the error
	 */
	public String getError() {
		return error;
	}


	/**
	 * @param error the error to set
	 */
	public void setError(String error) {
		this.error = error;
	}
	
	
	
	
	
}
